package org.example.jedis.hash;

import org.example.jedis.JedisDemo;
import org.junit.Test;

import java.util.HashMap;

/**
 * @author: tianhao.luo@hand-china.com 2021/5/25  16:00
 */
public class HashControlTest extends JedisDemo {


    /**
     * 命令:hset key field1 value1 field2 value2 ...
     * 对key对应的hash表存储field value映射关系
     */
    @Test
    public void hsetKeyFieldsValues(){
        String key1 = "h1";
        String field1 = "name";
        String field2 = "age";
        String value1 = "zhangsan";
        String value2 = "20";

        HashMap<String, String> map = new HashMap<>();
        map.put(field1,value1);
        map.put(field2,value2);
        delExistsKeyValue(key1);
        Long hset = jedis.hset(key1, map);
        LOGGER.info("key:{}存入field value映射结果为:{}",key1,hset);
    }


    /**
     * 命令:hget key field
     * 获取key对应的hash表中field对应的值
     */
    @Test
    public void hgetKeyField(){
        String key1 = "h1";
        String field1 = "name";
        String field2 = "age";
        String value1 = "zhangsan";
        String value2 = "20";

        HashMap<String, String> map = new HashMap<>();
        map.put(field1,value1);
        map.put(field2,value2);
        delExistsKeyValue(key1);
        Long hset = jedis.hset(key1, map);
        LOGGER.info("key:{}存入field value映射结果为:{}",key1,hset);

        String hget = jedis.hget(key1, field1);
        LOGGER.info("key:{}对应hash表中field:{}的value映射结果为:{}",key1,field1,hget);
    }

    /**
     * 命令:hmset key field1 value1 field2 value2 ...
     * 给key对应的hash表添加field value映射，与上面的hset一致
     */
    @Test
    public void hmsetKeyFieldsValues(){
        String key1 = "h1";
        String field1 = "name";
        String field2 = "age";
        String value1 = "zhangsan";
        String value2 = "20";

        HashMap<String, String> map = new HashMap<>();
        map.put(field1,value1);
        map.put(field2,value2);
        delExistsKeyValue(key1);
        String hmset = jedis.hmset(key1, map);
        LOGGER.info("key:{}存入field value映射结果为:{}",key1,hmset);
    }

    /**
     * 命令:hexists key field
     * 判断key对应的hash表中field属性是否存在；存在返回1，不存在返回0
     */
    @Test
    public void hexistsKeyField(){
        String key1 = "h1";
        String field1 = "name";
        String field2 = "age";
        String value1 = "zhangsan";
        String value2 = "20";

        HashMap<String, String> map = new HashMap<>();
        map.put(field1,value1);
        map.put(field2,value2);
        delExistsKeyValue(key1);
        String hmset = jedis.hmset(key1, map);
        LOGGER.info("key:{}存入field value映射结果为:{}",key1,hmset);

        Boolean hexists = jedis.hexists(key1, field1);
        LOGGER.info("key:{}对应的hash表是否存在field的结果为:{}",key1,hexists);

    }


    /**
     * 命令:hkeys key
     * 获取key对应的hash表中的所有field
     */
    @Test
    public void hkeysKey(){
        String key1 = "h1";
        String field1 = "name";
        String field2 = "age";
        String value1 = "zhangsan";
        String value2 = "20";

        HashMap<String, String> map = new HashMap<>();
        map.put(field1,value1);
        map.put(field2,value2);
        delExistsKeyValue(key1);
        String hmset = jedis.hmset(key1, map);
        LOGGER.info("key:{}存入field value映射结果为:{}",key1,hmset);

        for (String hkey : jedis.hkeys(key1)) {
            LOGGER.info("key:{}对应的hash表中的field为:{}",key1,hkey);
        }
    }


    /**
     * 命令:hkeys key
     * 获取key对应的hash表中的所有field
     */
    @Test
    public void hvalsKey(){
        String key1 = "h1";
        String field1 = "name";
        String field2 = "age";
        String value1 = "zhangsan";
        String value2 = "20";

        HashMap<String, String> map = new HashMap<>();
        map.put(field1,value1);
        map.put(field2,value2);
        delExistsKeyValue(key1);
        String hmset = jedis.hmset(key1, map);
        LOGGER.info("key:{}存入field value映射结果为:{}",key1,hmset);

        for (String hval : jedis.hvals(key1)) {
            LOGGER.info("key:{}对应的hash表中的value为:{}",key1,hval);
        }
    }


    /**
     * 命令:hincrby key field increment
     * 为key对应的field属性对应的value值加increment；返回的是value加increment之后的值。
     */
    @Test
    public void hincrbyKeyFieldIncrement(){
        String key1 = "h1";
        String field1 = "name";
        String field2 = "age";
        String value1 = "zhangsan";
        String value2 = "20";

        HashMap<String, String> map = new HashMap<>();
        map.put(field1,value1);
        map.put(field2,value2);
        delExistsKeyValue(key1);
        String hmset = jedis.hmset(key1, map);
        LOGGER.info("key:{}存入field value映射结果为:{}",key1,hmset);

        String hget1 = jedis.hget(key1, field2);
        LOGGER.info("key:{}对应hash表中field:{}的value映射结果为:{}",key1,field2,hget1);

        int increment = 10;
        Long aLong = jedis.hincrBy(key1, field2, increment);
        LOGGER.info("hincrBy的key:{}的执行结果为:{}",key1,aLong);

        String hget2 = jedis.hget(key1, field2);
        LOGGER.info("key:{}对应hash表中field:{}的value映射结果为:{}",key1,field2,hget2);
    }


    /**
     * 命令:hsetnx key field value
     * 为key的hash表添加field value映射表，如果已经存在了field，则返回0，不存在field，则添加并返回1
     */
    @Test
    public void hsetnxKeyFieldValue(){
        String key1 = "h1";
        String field1 = "name";
        String field2 = "age";
        String value1 = "zhangsan";
        String value2 = "20";
        delExistsKeyValue(key1);
        Long hsetnx1 = jedis.hsetnx(key1, field1, value1);
        LOGGER.info("key:{}存入field value映射结果为:{}",key1,hsetnx1);

        Long hsetnx2 = jedis.hsetnx(key1, field1, value1);
        LOGGER.info("key:{}存入field value映射结果为:{}",key1,hsetnx2);
    }
}
